
from rest_framework.filters import BaseFilterBackend
from django_filters.rest_framework import filterset
from django_filters.rest_framework import filters
from . import models

class limitCourseFilter(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        limit = request.query_params.get('limit')
        try:
            queryset = queryset[:int(limit)]
            return queryset
        except:
            return queryset


class CourseAdvancedFilter(filterset.FilterSet):

    min_price = filters.NumberFilter(field_name='price', lookup_expr='gte')
    max_price = filters.NumberFilter(field_name='price', lookup_expr='lte')

    class Meta:
        model = models.Course
        fields = ['course_category', 'min_price', 'max_price']
